package com.example.dcs;

import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.example.dcs.entity.UserInfo;
import com.mongodb.MongoClient;
import com.mongodb.client.ListCollectionsIterable;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;

@RestController
@SpringBootApplication
public class DcsApplication  extends WebMvcConfigurerAdapter {

    private static Logger log = LoggerFactory.getLogger(DcsApplication.class);

	public static void main(String[] args) {
		SpringApplication.run(DcsApplication.class, args);
		log.info("now start");
		MongoClient client = new MongoClient("localhost");
		MongoIterable<String> datbaseNames = client.listDatabaseNames();
		MongoDatabase dataBase = client.getDatabase("local");
		ListCollectionsIterable<Document> collections = dataBase.listCollections();
		boolean isExist = false;
		for(Document collection:collections){
		    if(collection.getString("name").equals("btc_trade")){
		        isExist = true;
		    }
		    log.info(collection.toJson());
		}
		if(!isExist){
		    dataBase.createCollection("btc_trade");
		}
//		while (it.hasNext()) {
//            type type = (type) it.next();
//
//        }
	}
	
	 @RequestMapping(value = {"/",""})
	    public String helloboot(){
	        return "hello boot !!" ;
	    }
	 
	 @RequestMapping(value="/{id}",method = RequestMethod.GET)
	    public UserInfo view(@PathVariable("id") Long id) {
	        UserInfo user = new UserInfo();
	        user.setId(id);
	        user.setName("name_"+id);
	        return user;  
	    }  
}
